package com.we.service.impl;

import com.we.dao.NoteDao;
import com.we.entity.Note;
import com.we.entity.PageBean;
import com.we.service.NoteService;
import com.we.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Description 笔记业务逻辑接口实现类
 * @Author cr7
 * @Date 20:46
 * @Version V1.0
 */
public class NoteServiceImpl implements NoteService {

    private SqlSession session = MybatisUtils.getSqlSession();
    private NoteDao dao = session.getMapper(NoteDao.class);

    /**
     * 查询所有笔记数量功能实现
     *
     * @return
     */
    @Override
    public int findNoteCount() {
        return dao.findNoteCount();
    }

    /**
     * 查询所有笔记功能(分页)实现
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Note> findAllNoteByPage(int currentPage, int pageSize) {
        PageBean<Note> pb = new PageBean<>();
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        int totalCount = dao.findNoteCount();
        pb.setTotalCount(totalCount);
        //设置当前页显示的数据集合
        int start = (currentPage - 1) * pageSize;//开始的记录数
        List<Note> list = dao.findByPage(start, pageSize);
        pb.setList(list);
        //设置总页数 = 总记录数/每页显示条数
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pb.setTotalPage(totalPage);
        return pb;
    }

    /**
     * 根据关键字查询所有笔记功能实现(分页搜索)
     * @param content
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Note> findAllNoteByPage(String content, int currentPage, int pageSize) {
        PageBean<Note> pb = new PageBean<>();
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        int totalCount = dao.searchNoteCount(content);
        pb.setTotalCount(totalCount);
        //设置当前页显示的数据集合
        int start = (currentPage - 1) * pageSize;//开始的记录数
        List<Note> list = dao.searchNoteByPage(content,start, pageSize);
        pb.setList(list);
        //设置总页数 = 总记录数/每页显示条数
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pb.setTotalPage(totalPage);
        return pb;
    }

    /**
     * 保存笔记功能实现
     * @param note
     * @return
     */
    @Override
    public int addNote(Note note) {
        return dao.addNote(note);
    }

    /**
     * 查询最新博客功能实现
     * @return
     */
    @Override
    public List<Note> findNoteByTime() {
        return dao.findNoteByTime();
    }

    /**
     * 查询最热博客功能实现
     * @return
     */
    @Override
    public List<Note> findNoteByVisit() {
        return dao.findNoteByVisit();
    }

    /**
     * 根据id查询博客详情功能实现
     * @param noteID
     * @return
     */
    @Override
    public Note findNoteById(int noteID) {
        return dao.findNoteById(noteID);
    }

    /**
     * 查询分类下的所有笔记功能(分页)实现
     * @param categoryName
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Note> findNoteByCategory(String categoryName, int currentPage, int pageSize) {
        PageBean<Note> pb = new PageBean<>();
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        int totalCount = dao.findNoteByCategoryCount(categoryName);
        pb.setTotalCount(totalCount);
        //设置当前页显示的数据集合
        int start = (currentPage - 1) * pageSize;//开始的记录数
        List<Note> list = dao.findNoteByCategory(categoryName,start, pageSize);
        pb.setList(list);
        //设置总页数 = 总记录数/每页显示条数
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pb.setTotalPage(totalPage);
        return pb;
    }

}
